﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using DTO;
namespace DAO
{
    public class PhieuNhapDAO
    {
        DataProvider dp;
        public PhieuNhapDAO()
        {
            dp = new DataProvider();
        }
        public DataTable LayDSPhieuNhap()
        {
            DataTable table = new DataTable();
            string sql = "select * from PhieuNhap";
            table = dp.LayBangDuLieu(sql);
            return table;
        }
        public bool XoaPhieuNhap(int maphieunhap)
        {
            bool flag = true;
            string sql1 = "delete from ChiTietPhieuNhap where maphieunhap = " + maphieunhap + "";
            if (!dp.ThucHienCauTruyVan(sql1)) flag = false;
            string sql2 = "delete from PhieuNhap where maphieunhap =" + maphieunhap + "";
            if (!dp.ThucHienCauTruyVan(sql2)) flag = false;
            return flag;
        }
        public PhieuNhapDTO LayPhieuNhapSach(int maphieunhap)
        {
            PhieuNhapDTO pndto = new PhieuNhapDTO();
            string sql = "select * from PhieuNhap where maphieunhap =" + maphieunhap + "";
            DataTable table = new DataTable();
            table = dp.LayBangDuLieu(sql);
            DataRow dtr = table.Rows[0];
            pndto.Maphieunhap = int.Parse(dtr[0].ToString());
            pndto.Ngaynhap = DateTime.Parse(dtr[1].ToString());
            return pndto;
        }
        public int LayMaPhieuTuDong()
        {
            int maphieu = 0;
            string sql = "select * from PhieuNhap";
            DataTable table = new DataTable();
            table = dp.LayBangDuLieu(sql);
            DataRow dr = table.Rows[table.Rows.Count - 1];
            maphieu = int.Parse(dr[0].ToString());
            return maphieu;
        }
        public bool ThemPhieuNhap(PhieuNhapDTO pndto)
        {
            bool flag = true;
            string sql = "insert into PhieuNhap(ngaynhap) values (" + pndto.Ngaynhap.ToShortDateString() + ")";
            if (!dp.ThucHienCauTruyVan(sql)) flag = false;
            return flag;
        }
        public DataTable TimKiemPhieuNhap(DateTime dt)
        {
            DataTable table = new DataTable();
            string sql = "select * from PhieuNhap where ngaynhap =" + dt.ToShortDateString() + "";
            table = dp.LayBangDuLieu(sql);
            return table;
        }
    }
}
